/* 
 * @author Mabel Xu
 */

import info.gridworld.actor.Bug;

/**
 * A <code>ZBug</code> traces out a z of a given size. <br />
 */
public class ZBug extends Bug
{
	private int steps;
    private int sideLength;
    private boolean done;

    /**
     * Constructs a z bug that traces a z of a given side length
     * @param length the side length
     */
    public ZBug(int length)
    {
    	steps = 0;
        sideLength = length;
    }

    /**
     * Moves to the next location of the z.
     */
    public void act()
    {
    	// plan of action ... "manually do everything"
    	if (!done)
    	{
    		// first, turn so its facing right
    		this.setDirection(90);
    		
    		// then, move correct number of steps
    		while (steps < sideLength && this.canMove())
    		{
    			this.move();
    			steps++;
    		}
    		// reset steps counter
    		steps = 0;
    		
    		// next, turn to face down and left
    		if (this.canMove())
    			this.setDirection(225);
    		
    		// move correct number of steps
    		while (steps < sideLength && this.canMove())
    		{
    			this.move();
    			steps++;
    		}
    		// reset steps counter
    		steps = 0;
    		
    		// turn to face right again
    		if (this.canMove())
    			this.setDirection(90);
    		
    		// move correct number of steps
    		while (steps < sideLength && this.canMove())
    		{
    			this.move();
    			steps++;
    		}
    		// reset steps counter
    		steps = 0;
    		
    		// tada, z is done yay!
    		done = true;
    	}
    	else
    	{
    		// dont do anything if done
    	}
    }
}